import { Component, OnInit, Input, EventEmitter,
Output } from '@angular/core';
import {Booked, BookedResult, User} from '../entity/TT';
import {RequestReplyService} from '../services/request-reply.service';
import {Utils} from '../common/Common';


export class OpenEvent {
  isOpen: boolean;
  booked: Booked;
}

@Component({
  selector: 'app-booked',
  templateUrl: './booked.component.html',
  styleUrls: ['./booked.component.scss']
})
export class BookedComponent implements OnInit {

  @Input()
  booked: Booked;

  @Output() onOpen = new EventEmitter<OpenEvent>();

  isOpen = false;

  constructor(private requestService:RequestReplyService) { }

  ngOnInit() {
  }

  openBox() {
    this.isOpen = !this.isOpen;

    const evt: OpenEvent = {
      isOpen: this.isOpen,
      booked: this.booked
    } ;

    this.onOpen.next(evt);

    if(this.isOpen){

    }
  }

  isCompleted(): boolean {
    return this.booked.result.name === BookedResult.completed;
  }

  getStatus(): string {
    const me: User = this.requestService.LoadMe();
    const isTeacher = Utils.isTeacher(me)
    const st = this.booked.statuses[me.uin]
    if( st ) {
      return isTeacher ? st.name : st.text;
    }
    return isTeacher ? this.booked.result.name : this.booked.result.text;
  }

}
